#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int			 g_var = 6;
char		 g_buf[] = "A string write to stdout.\n";

int main(int argc, char **argv)
{
	pid_t			pid;
	int				var = 88;
	int				fd = -1;
	FILE			*p_file = NULL;
	char			buf[1024];

	if( write(STDOUT_FILENO,g_buf,sizeof(g_buf)) < 0)
	{
		printf("wirte error : %s\n",strerror(errno));
		return -1;
	}
	if((fd = open("TEST_FORK", O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
	{
		printf("open error : %s \n", strerror(errno));
		return -2;
	}

	printf("befor fork\n");

	pid = fork();
	if(pid < 0)
	{
		printf("fork create child process fialure : %s \r\n",strerror(errno));
		return -3;
	}	
	else if(pid == 0)
	{
		printf("child process pid %d parent ppid %d \n",getpid(),getppid());
		g_var++;
		var++;
		printf("g_var = %d , var = %d \n",g_var ,var);

		execl("/sbin/ifconfig", "ifconfig", "eth0", NULL);

	}
	else if(pid > 0)
	{
		printf("parent %d \n",getpid());
		printf("g_var = %d , var = %d \n",g_var ,var);
		sleep(5);

		p_file = popen("ifconfig eth0","r");
		if(!p_file)
		{
			printf("popen error :%s \n", strerror(errno));
			return -5;
		}

		while(fgets(buf, sizeof(buf), p_file) != NULL)
		{
			printf(" %s ",buf);
			write(fd, buf, strlen(buf));
		}

		pclose(p_file);
		close(fd);
	}
	return 0;
}







